﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace My_TV_Manager.Model
{
    public class SearchResult : IComparable
    {
        #region Private Data

        private string seriesName;
        private int seriesId;
        private int yearStarted;
        private double rating;
        private int numRatings;
        private int relevance;
        private Bitmap banner;

        private SearchResultSortCategory sortCategory;

        #endregion

        #region Properties

        public string SeriesName
        {
            get { return seriesName; }
            set { seriesName = value; }
        }
        public int SeriesId
        {
            get { return seriesId; }
            set { seriesId = value; }
        }
        public int YearStarted
        {
            get { return yearStarted; }
            set { yearStarted = value; }
        }
        public double Rating
        {
            get { return rating; }
            set { rating = value; }
        }
        public int TotalRatings
        {
            get { return numRatings; }
            set { numRatings = value; }
        }
        public int Relevance
        {
            get { return relevance; }
            set { relevance = value; }
        }
        public Bitmap Banner
        {
            get { return banner; }
            set { banner = value; }
        }

        public SearchResultSortCategory SortCategory
        {
            get { return sortCategory; }
            set { sortCategory = value; }
        }

        #endregion

        #region Constructors

        // Default constructor
        public SearchResult()
        {
            this.seriesName = "";
            this.seriesId = -1;
            this.yearStarted = -1;
            this.rating = 0.0;
            this.numRatings = 0;
            this.relevance = 0;
            this.banner = null;

            this.sortCategory = SearchResultSortCategory.Relevance;
        }

        // Constructor to use input data
        public SearchResult(string seriesName, int seriesId, int yearStarted,
                            double rating, int numRatings, Bitmap banner)
        {
            this.seriesName = seriesName;
            this.seriesId = seriesId;
            this.yearStarted = yearStarted;
            this.rating = rating;
            this.numRatings = numRatings;
            this.banner = banner;

            this.sortCategory = SearchResultSortCategory.Relevance;
        }

        #endregion

        #region Functions

        public void Print()
        {
            Console.Out.WriteLine(seriesName + " : " + seriesId +
                                  "\n\tYear Started: " + yearStarted +
                                  "\n\tRating: " + rating + 
                                  "\n\tTotal Ratings: " + numRatings +
                                  "\n\tBanner: " + (banner != null));  
        }

        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {
            if (obj is SearchResult)
            {
                SearchResult result = (SearchResult)obj;

                switch (sortCategory)
                {
                    case SearchResultSortCategory.Alphabetic:
                        return seriesName.CompareTo(result.SeriesName);
                    case SearchResultSortCategory.ID:
                        return seriesId.CompareTo(result.SeriesId);
                    case SearchResultSortCategory.Relevance:
                        return relevance.CompareTo(result.Relevance);
                    case SearchResultSortCategory.Rating:
                        return rating.CompareTo(result.Rating);
                    case SearchResultSortCategory.YearStarted:
                        return yearStarted.CompareTo(result.YearStarted);
                    default:
                        return seriesName.CompareTo(result.SeriesName);
                }
            }
            else
            {
                throw new ArgumentException("Object is not of type SearchResult");
            }
        }

        #endregion
    }

    public enum SearchResultSortCategory
    {
        Alphabetic,
        ID,
        Rating,
        Relevance,
        YearStarted
    }
}
